<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
require_once('php_shared/session_to_db.php');

// Self uid and username
$username = "";
$uid = "";
// Conversation partner's uid and username
$fname = "";
$fuid = "";
// Mysql result array of messages, for later use
$msgs;

session_start();

if( isset($_SESSION['user_id'], $_REQUEST['fuid']) )
{
	if( ($_SESSION['user_id'] == null) || ($_SESSION['user_id'] == '') )
	{
		alert_and_exit("You need to be logged in to view logs.");
	}
	else
	{
		$uid = $_SESSION['user_id'];
		$fuid = $_REQUEST['fuid'];
		
		if( $_SERVER['REQUEST_METHOD'] == 'POST' )
		{
			if( isset($_POST['start_time'], $_POST['end_time']) )
			{
				$db_man = new DbUtils();
					
				if( $db_man->connect() )
				{
					$uid = $db_man->set_var($_SESSION['user_id']);
					$fuid = $db_man->set_var($_REQUEST['fuid']);
					$stime = $db_man->set_var($_POST['start_time']);
					$etime = $db_man->set_var($_POST['end_time']);
					$sql = "SELECT r.username, c.to_uid, c.message, c.msg_time FROM conversation c, registered_user r WHERE ( (c.from_uid=$uid AND c.to_uid=$fuid) OR (c.from_uid=$fuid AND c.to_uid=$uid) ) AND c.msg_time>=$stime AND c.msg_time<=$etime AND c.from_uid=r.id ORDER BY c.msg_time ASC";
					
					$result = $db_man->query($sql);
					if( !$result )
					{
						alert_and_exit("An error occured while communicating with database. Please try starting conversation again later.");
					}else{
						$msgs = $result;
					}
					
				}else{
					alert_and_exit("An error occured while communicating with database. Please try starting conversation again later.");
				}
			}
		}
	}
}else{
	alert_and_exit("You need to be logged in to view logs.");
}

function alert_and_exit($alert_msg)
{
	print_javascript("alert('$alert_msg');window.close();");;
}

function print_javascript($script)
{
	print "<script type='text/javascript'>$script</script>";
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Conversation Log | CSE315 IM</title>
    <link href="css/shared.css" rel="stylesheet" type="text/css" />
    <link href="css/speech_bubble.css" rel="stylesheet" type="text/css" />
    <link href="css/log.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/speech_bubble.js"></script>
    <script type="text/javascript" src="js/refresh_timer.js"></script>
    <script type="text/javascript" src="js/ie_textContent_fix.js"></script>
    <script type="text/javascript" src="js/datetimepicker_css.js"></script>
</head>

<body>
	
    <div class="search_area">
        <form action="log.php?fuid=<?php print $fuid ?>" method="post">
        	<table width="100%">
            <tbody>
        	<tr>
            	<td><label>Start Date/Time:</label></td>
            	<td>
                    <a href="javascript:NewCssCal('start_time_input','yyyymmdd','arrow',true,24,false)"> 
					<img src="img/datetimepicker/cal.gif" width="16" height="16" alt="Pick a date"></a>
                    <input class="date_input" type="text" name="start_time" id="start_time_input" readonly="readonly" />
                </td>
                <td></td>
            </tr><tr>
            	<td><label>End Date/Time:</label></td>
            	<td>
                	<a href="javascript:NewCssCal('end_time_input','yyyymmdd','arrow',true,24,false)"> 
					<img src="img/datetimepicker/cal.gif" width="16" height="16" alt="Pick a date"></a>
                    <input class="date_input" type="text" name="end_time" id="end_time_input" readonly="readonly" />
                </td>
           		<td><input class="search_button" type="submit" value="Search" name="send_msg_button" /></td>
            </tr>
            </tbody>
            </table>
        </form>
    </div>
    
	<div class="msg_show_area" id="display_area"></div>
	
    <div class="conv" style="display:none;" id="conv_template">
        <div class="conv_title_top"><b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b></div>
        <div class="conv_header"></div><!-- end #conv_header -->
        <div class="conv_title_bottom"><b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b></div>
        <div class="prompt_content conv_content"></div><!-- end #conv_content -->
        <b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b>
    </div><!-- end #conv -->
    
    <?php
		// Append all message bubbles to the message show area
		if( isset($msgs) )
		{
			while( $row = mysql_fetch_assoc($msgs) )
			{
				if( $uid == $row['to_uid'] )
				{
					$incoming = "true";
					
				}else{
					$incoming = "false";
				}
				$source = $row['username'];
				
				print_javascript("create_conv_bubble('$source','".$row['message']."', '".$row['msg_time']."', $incoming);");
			}
		}
	?>
</body>
</html>